define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bonus/index' + location.search,
                    add_url: 'bonus/add',
                    edit_url: 'bonus/edit',
                    del_url: 'bonus/del',
                    multi_url: 'bonus/multi',
                    import_url: 'bonus/import',
                    table: 'bonus',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle:false,
                search:false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'image', title: '图标', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'bonus_money', title: __('Bonus_money'), operate:'BETWEEN'},
                        // {field: 'postsaux_id', title: __('Postsaux_id')},
                        {field: 'postsaux.name', title: '可兑换商品', operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('change','.selectpicker',function(ee){
                var type = $("#c-type").val();
                if(type == 1){
                    $("#bonus_money").show();
                    $("#money").show();
                    $("#postsaux_id").hide();
                }else{
                    $("#bonus_money").hide();
                    $("#money").hide();
                    $("#postsaux_id").show();
                }
                console.log(type);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on('change','.selectpicker',function(ee){
                var type = $("#c-type").val();
                if(type == 1){
                    $("#bonus_money").show();
                    $("#money").show();
                    $("#postsaux_id").hide();
                }else{
                    $("#bonus_money").hide();
                    $("#money").hide();
                    $("#postsaux_id").show();
                }
                console.log(type);
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
